package engine.newUi
{
	import engine.data.ModelLocator;
	import engine.logic.ChatFilterLogic;
	import engine.logic.login.PlayCDTimerLogic;
	import engine.net.datatype.WString;
	import engine.net.event.CustomMsgEvent;
	import engine.net.messages.CustomToClientFlag;
	import engine.net.messages.CustomToServerFlag;
	import engine.net.mgr.SystemSender;
	import engine.ui.UIManage;
	import engine.ui.chat.ChatUI;
	import engine.ui.lite.BaseUI;
	
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.events.TextEvent;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	
	import mx.utils.StringUtil;
	
	import newx.StageInfo;
	import newx.com.config.StaticIni;
	import newx.com.control.button.NxButton;
	import newx.com.control.text.NxLabel;
	import newx.com.control.window.NxWindowEvent;
	
	/**
	 * 
	 * @author gaoyq
	 * 
	 */
	public class FamilyInviteUI extends BaseUI
	{
		private static var _self			:FamilyInviteUI;
		
		public		var		_description	:NxLabel;
		
		public		var		_okBtn			:NxButton;
		public		var		_noBtn			:NxButton;
		public 		var 	timer			:NxLabel;
		public     var 	countLab		:NxLabel;
		private	var		_inputText		:TextField;
		private	var		_txtLeng		:int;
		
		private 	var 	_oldtxtStr:String = "";
		
		private	var		model			:ModelLocator;
		public function FamilyInviteUI()
		{
			super();
		}
		
		public static function getInstance():FamilyInviteUI
		{
			if(_self==null)
			{
				_self = new FamilyInviteUI();
			}
			return _self;
		}
		
		override public function Init():void
		{
			_inputText						=	new TextField();
			_inputText		.background 	= 	true;
			_inputText		.type 			= 	TextFieldType.INPUT;
			_inputText		.backgroundColor= 	0x012635;
			_inputText		.textColor		=	0xffffff;
			_inputText		.multiline		=	true;
			_inputText		.wordWrap		=	true;
			_inputText		.maxChars		=	50;
			_inputText		.width			=	280;
			_inputText		.height			=	60;
			_inputText		.x				=	20;
			_inputText		.y				=	60;
			PlayCDTimerLogic.getInstance().resgis(17,setTimerlab);
			var testStr		:String			=	"";
			for(var i:int = 0; i < 150; i ++)
			{
				testStr		=	testStr.concat("a");
			}
			_inputText		.text			=	testStr;
			_txtLeng						=	_inputText.textWidth;
			_inputText		.text			=	"";
			
			//			_inputText		.addEventListener(	TextEvent.TEXT_INPUT,	onTxtInput);
			_inputText		.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
			_inputText		.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
			
			windowBG		.addChild(_inputText);
			model = ModelLocator.getInstance();
			//反叛选择事件，弹出选择框
			model			.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_GUILD_RECRUIT_INFO),onInviteHandle)
		}
		
		private function setTimerlab(val:int,col:int):void
		{
			if(val<=0)
			{
				_okBtn.label = StaticIni.getSwfLangStr("UIStr_1000009");
				_okBtn.enabled = true;
			}else
			{
				_okBtn.label = PlayCDTimerLogic.getInstance().getTimerStr(val);
				_okBtn.enabled = false;
			}
			
		}
		public function onInviteHandle(e:CustomMsgEvent):void
		{
			if(e.pList[0] == "" || e.pList[0] == null)
			{
				_inputText.text = StaticIni.getSwfLangStrVar("UIString_1000465",
					[FamilyInfoUI.getInstance()._familyLevel.text,FamilyInfoUI.getInstance()._familyName.text]);
			}else
			{
				_inputText.text = e.pList[0];
				//onKeyUp();
			}
			this.showOrHide();
		}
		
		private function onInit():void
		{
			
		}
		

		
		private function onOkBtnClick(e:MouseEvent):void
		{
			var msg:String = ChatUI.getInstance().htmlCode(_inputText.text);
			msg = ChatFilterLogic.getInstance().filter(msg);
			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_GUILD_MEMBER_RECRUIT,new WString(msg));
			onClickClose();
		}
		
		private function onNoBtnClick(e:MouseEvent):void
		{
			onClickClose();
		}
		
		private function onKeyDown(evt : KeyboardEvent):void
		{
			var num:int			=	_inputText.length ;
			countLab.htmlText	=	StaticIni.getSwfLangStrVar("UIString_1000109",[(50-num)]);
		}
		private function onKeyUp(evt : KeyboardEvent = null):void
		{
			var num:int			=	_inputText.length ;
			countLab.htmlText	=	StaticIni.getSwfLangStrVar("UIString_1000109",[(50-num)]);
		}
		
		override protected	function	BeforeShow():void
		{	
			windowBG		.addEventListener(	NxWindowEvent.WINDOW_CLOSE,onClickClose);
			_okBtn			.addEventListener(  MouseEvent.CLICK,	onOkBtnClick);
			_noBtn			.addEventListener(  MouseEvent.CLICK,	onNoBtnClick);
			_oldtxtStr = StringUtil.trim(_inputText.text);
			onKeyUp();
		}
		override protected function AfterHide():void
		{
			windowBG		.removeEventListener(	NxWindowEvent.WINDOW_CLOSE,onClickClose);
		}
		
		private function onClickClose(e:NxWindowEvent=null):void
		{
			hide();
		}
		
	}
}